package com.orangecy.completablefuture;

import com.orangecy.common.exception.OrangeCyException;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author : orangeCy.奕
 * @Time : 2021/10/6
 * @desc : 猎豹养成产品源码
 */
public class CtfNote {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 任务A执行完执行B ，执行B的时候不需要依赖A的接口 同时B也不需要返回结果
         */
        CompletableFuture.supplyAsync(() -> "resultA").thenRun(() -> {});
        /**
         * 任务A执行完执行B ，执行B的时候需要依赖A的接口 同时B也不需要返回结果
         */
        CompletableFuture.supplyAsync(() -> "resultA").thenAccept(resultA -> {});

        /**
         * 任务A执行完执行B ，执行B的时候需要依赖A的接口 同时B也需要返回结果
         */
        CompletableFuture.supplyAsync(() -> "resultA").thenApply(resultA -> "resultB");

        //Question? thenCompose & supplyAsync 区别?

        //-----thenCompose
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> "xiaoyi")
                .thenCompose(s -> CompletableFuture.supplyAsync(() -> "welcome" + s));
        //两种返回的 对返回值处理计算
        //异常处理 eg

        CompletableFuture<String> cxf = CompletableFuture.supplyAsync(() -> {
            throw new OrangeCyException("cxf");
        }).exceptionally(e -> "我出错了").thenApply(res -> res + " 哦?")
                .thenApply(resA -> resA + " 真菜");
        System.out.println(cxf.get());
    }
}